package com.ruoyi.purchase.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.purchase.domain.ChangeDetail;
import com.ruoyi.purchase.domain.OrderDetail;
import com.ruoyi.purchase.domain.PurchaseOrder;
import com.ruoyi.purchase.domain.PurchasePlan;
import com.ruoyi.purchase.mapper.*;
import com.ruoyi.purchase.service.IChangeDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * detailService业务层处理
 * 
 * @author kevinness
 * @date 2025-01-05
 */
@Service
public class ChangeDetailService implements IChangeDetailService
{
    @Autowired
    private ChangeDetailMapper changeDetailMapper;

    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private PurchasePlanMapper purchasePlanMapper;

    @Autowired
    private PlanOrderLinkMapper planOrderLinkMapper;

    /**
     * 查询detail
     * 
     * @param orderNo detail主键
     * @return detail
     */
    @Override
    public List<ChangeDetail> selectChangeDetailByOrderNo(String orderNo)
    {
        return changeDetailMapper.selectChangeDetailByOrderNo(orderNo);
    }

    /**
     * 查询detail列表
     * 
     * @param changeDetail detail
     * @return detail
     */
    @Override
    public List<ChangeDetail> selectChangeDetailList(ChangeDetail changeDetail)
    {
        return changeDetailMapper.selectChangeDetailList(changeDetail);
    }

    /**
     * 新增detail
     * 
     * @param changeDetail detail
     * @return 结果
     */
    @Override
    public int insertChangeDetail(ChangeDetail changeDetail)
    {
        return changeDetailMapper.insertChangeDetail(changeDetail);
    }

    /**
     * 修改detail
     * 
     * @param changeDetail detail
     * @return 结果
     */
    @Override
    public int updateChangeDetail(ChangeDetail changeDetail)
    {
        return changeDetailMapper.updateChangeDetail(changeDetail);
    }

    /**
     * 批量删除detail
     * 
     * @param orderNos 需要删除的detail主键
     * @return 结果
     */
    @Override
    public int deleteChangeDetailByOrderNos(String[] orderNos)
    {
        return changeDetailMapper.deleteChangeDetailByOrderNos(orderNos);
    }

    /**
     * 通过id批量删除
     *
     * @param ids 需要删除的变更id集合
     * @return 结果
     */
    @Override
    public int deleteChangeDetailByIds(Integer[] ids){
        return changeDetailMapper.deleteChangeDetailByIds(ids);
    }

    /**
     * 删除detail信息
     * 
     * @param orderNo detail主键
     * @return 结果
     */
    @Override
    public int deleteChangeDetailByOrderNo(String orderNo)
    {
        return changeDetailMapper.deleteChangeDetailByOrderNo(orderNo);
    }

    /**
     *  通过修改申请
     *
     * @param id 变更编号
     * @return 结果
     */
    @Override
    public int passChangeDetail(int id){
        ChangeDetail changeDetail = changeDetailMapper.selectChangeDetailById(id);

        //更新订单详情
        List<OrderDetail> details = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            details = objectMapper.readValue(changeDetail.getOrderDetails()
                    , new TypeReference<List<OrderDetail>>(){});
        } catch (Exception e) {
            e.printStackTrace();
        }

        //更新采购计划
        for (OrderDetail orderDetail : details) {
            List<OrderDetail> oldDetails =
                    orderDetailMapper.selectOrderDetailList(orderDetail);
            if(!Objects.equals(oldDetails.get(0).getQuantity(), orderDetail.getQuantity())){
                Double delta = orderDetail.getQuantity() - oldDetails.get(0).getQuantity();
                String planNo = planOrderLinkMapper.selectPlanNoByRawProductBatchNo(orderDetail.getRawProductBatch());
                PurchasePlan purchasePlan = purchasePlanMapper.selectPurchasePlanByPlanNo(planNo);
                purchasePlan.setPurchaseQuantity(purchasePlan.getPurchaseQuantity() + delta);
                purchasePlan.setPurchaseStatusByPurchaseQuantity();
                purchasePlanMapper.updatePurchasePlan(purchasePlan);
            }
        }

        for (OrderDetail orderDetail : details) {
            orderDetailMapper.updateOrderDetail(orderDetail);
        }

        //更新订单
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedNow = now.format(formatter);
        PurchaseOrder purchaseOrder = new PurchaseOrder(changeDetail);
        purchaseOrder.setModifiedDate(formattedNow);
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }
}
